package com.ideaaedi.springcloud.jd.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ideaaedi.springcloud.jd.commonds.entity.user.po.SysMenuApiRePO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysMenuBindApiReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysApiListRespVO;
import com.ideaaedi.springcloud.jd.commonspring.mapper.SysMenuApiReMapper;
import com.ideaaedi.springcloud.jd.commonspring.mybatisplus.ServiceImplExt;
import com.ideaaedi.springcloud.jd.user.service.SysApiResourceService;
import com.ideaaedi.springcloud.jd.user.service.SysMenuApiReService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统-菜单资源中间 服务实现类
 * </p>
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.5.B
 */
@Service
public class SysMenuApiReServiceImpl extends ServiceImplExt<SysMenuApiReMapper, SysMenuApiRePO> implements SysMenuApiReService {
    
    @Resource
    SysApiResourceService sysApiResourceService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SysApiListRespVO> bindApi(SysMenuBindApiReqVO req) {
        Long menuId = req.getMenuId();
        Set<Long> apis = req.getApiIds();
        // 删除原来的
        remove(new LambdaQueryWrapper<SysMenuApiRePO>()
                .eq(SysMenuApiRePO::getMenuId, menuId)
        );
        
        // 添加新的
        if (!CollectionUtils.isEmpty(apis)) {
            List<SysMenuApiRePO> list = apis.stream().map(endpointId -> {
                SysMenuApiRePO re = new SysMenuApiRePO();
                re.setId(IdWorker.getId());
                re.setMenuId(menuId);
                re.setApiId(endpointId);
                return re;
            }).collect(Collectors.toList());
            saveBatch(list);
        }
        return queryBindApi(menuId);
    }
    
    @Override
    public List<SysApiListRespVO> queryBindApi(Long menuId) {
        Set<Long> apiIds = list(new LambdaQueryWrapper<SysMenuApiRePO>()
                .eq(SysMenuApiRePO::getMenuId, menuId)
        ).stream()
                .map(SysMenuApiRePO::getApiId)
                .collect(Collectors.toSet());
        
        List<SysApiListRespVO> list = Collections.emptyList();
        if (!CollectionUtils.isEmpty(apiIds)) {
            list = sysApiResourceService.listByIds(apiIds).stream().map(x -> {
                SysApiListRespVO resp = new SysApiListRespVO();
                resp.setId(x.getId());
                resp.setName(x.getName());
                resp.setPath(x.getPath());
                resp.setRequestMethod(x.getRequestMethod());
                resp.setRemark(x.getRemark());
                return resp;
            }).collect(Collectors.toList());
        }
        return list;
    }
}
